<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="栏目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入栏目名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="栏目状态" clearable style="width: 200px">
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:column:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="columnList"
      row-key="columnId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="栏目名称" width="200"></el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="100">
        <template #default="scope">
          <dict-tag :options="column_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="栏目值" align="center" prop="value" width="120" />
      <el-table-column label="英文标识" align="center" prop="typeEn" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.typeEn" type="info">{{ scope.row.typeEn }}</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="模型类型" align="center" prop="modelType" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.modelType === '1'">视频</el-tag>
          <el-tag type="success" v-else-if="scope.row.modelType === '2'">文章</el-tag>
          <el-tag type="warning" v-else-if="scope.row.modelType === '8'">演员</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center" prop="icon" width="80">
        <template #default="scope">
          <el-image
            v-if="scope.row.icon"
            :src="scope.row.icon"
            style="width: 30px; height: 30px"
            fit="contain"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column prop="order" label="排序" width="80"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['basic:column:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['basic:column:add']">新增</el-button>
          <el-button v-if="scope.row.parentId != 0" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['basic:column:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改栏目对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="columnRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item label="上级栏目" prop="parentId">
              <el-tree-select
                v-model="form.parentId"
                :data="columnOptions"
                :props="{ value: 'columnId', label: 'name', children: 'children' }"
                value-key="columnId"
                placeholder="选择上级栏目"
                check-strictly
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="栏目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入栏目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio
                  v-for="dict in column_type"
                  :key="dict.value"
                  :value="dict.value"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型类型" prop="modelType">
              <el-select v-model="form.modelType" placeholder="请选择模型类型">
                <el-option label="视频" value="1" />
                <el-option label="文章" value="2" />
                <el-option label="演员" value="8" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="栏目值" prop="value">
              <el-input v-model="form.value" placeholder="如：movie、tv" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文标识" prop="typeEn">
              <el-input v-model="form.typeEn" placeholder="如：Movie、TV" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="order">
              <el-input-number v-model="form.order" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :value="dict.value"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="栏目图标" prop="icon">
              <el-input v-model="form.icon" placeholder="请输入图标URL" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Column">
import { listColumn, getColumn, delColumn, addColumn, updateColumn, listColumnExcludeChild } from "@/api/basic/column";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { Picture } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();
const { sys_normal_disable, column_type } = proxy.useDict('sys_normal_disable', 'column_type');

const columnList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const columnOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    name: undefined,
    status: undefined
  },
  rules: {
    parentId: [{ required: true, message: "上级栏目不能为空", trigger: "blur" }],
    name: [{ required: true, message: "栏目名称不能为空", trigger: "blur" }],
    value: [{ required: true, message: "栏目值不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询栏目列表 */
function getList() {
  loading.value = true;
  listColumn(queryParams.value).then(response => {
    columnList.value = proxy.handleTree(response.data, "columnId");
    loading.value = false;
  });
}

/** 查询栏目下拉树结构 */
function getTreeselect() {
  listColumn({}).then(response => {
    columnOptions.value = [];
    const column = { columnId: 0, name: '主类目', children: [] };
    column.children = proxy.handleTree(response.data, "columnId");
    columnOptions.value.push(column);
  });
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  proxy.$nextTick(() => {
    refreshTable.value = true;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    columnId: undefined,
    parentId: undefined,
    name: undefined,
    type: undefined,
    value: undefined,
    typeEn: undefined,
    modelType: undefined,
    icon: undefined,
    order: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("columnRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != undefined && row.columnId) {
    form.value.parentId = row.columnId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加栏目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getColumn(row.columnId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改栏目";
    // 查询上级栏目选项（排除当前节点和子节点）
    listColumnExcludeChild(row.columnId).then(response => {
      columnOptions.value = [];
      const column = { columnId: 0, name: '主类目', children: [] };
      column.children = proxy.handleTree(response.data, "columnId");
      columnOptions.value.push(column);
    });
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["columnRef"].validate(valid => {
    if (valid) {
      if (form.value.columnId != undefined) {
        updateColumn(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addColumn(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除栏目名称为"' + row.name + '"的数据项？').then(function() {
    return delColumn(row.columnId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 14px;
}
</style>
